import Animation from '../base/animation'
import DataBus   from '../databus'

const BOOM_IMG_SRC = 'images/boom.png'
const BOOM_WIDTH   = 60
const BOOM_HEIGHT  = 60

const __ = {
  speed: Symbol('speed')
}

let databus = new DataBus()

function rnd(start, end){
  return Math.floor(Math.random() * (end - start) + start)
}

export default class Booms extends Animation {
  constructor() {
    super(BOOM_IMG_SRC, BOOM_WIDTH, BOOM_HEIGHT)
  }

  init(speed) {
    this.x = rnd(0, window.innerWidth - BOOM_WIDTH)
    this.y = -this.height

    this[__.speed] = speed
    this.visible = true
  }
  // 每一帧更新炸弹的位置
  update() {
    this.y += this[__.speed]
    // 对象回收
    if ( this.y > window.innerHeight + this.height )
      databus.removeBooms(this)
  }
}
